<template>
  <div class="user-profile">
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      right-text="保存"
      left-arrow
      @click-left="$router.back()"
    />
    <van-cell title="头像" @click="$refs.file.click()" is-link>
      <van-image fit="cover" class="avatar" round :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      @click="isUpdateNameShow = true"
      :value="user.name"
      is-link
    />
    <van-cell
      title="性别"
      @click="isUpdateGenderShow = true"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
    />
    <van-cell
      title="生日"
      @click="isUpdateBirthdayShow = true"
      :value="user.birthday"
      is-link
    />

    <!-- 编辑昵称弹层 -->
    <van-popup
      v-model="isUpdateNameShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <UpdateName
        v-if="isUpdateNameShow"
        v-model="user.name"
        @close="isUpdateNameShow = false"
      />
    </van-popup>
    <!-- 编辑性别弹层 -->
    <van-popup v-model="isUpdateGenderShow" position="bottom">
      <UpdateGender
        v-if="isUpdateGenderShow"
        v-model="user.gender"
        @close="isUpdateGenderShow = false"
      />
    </van-popup>
    <!-- 编辑生日弹层 -->
    <van-popup v-model="isUpdateBirthdayShow" position="bottom">
      <UpdateBirthday
        v-if="isUpdateBirthdayShow"
        v-model="user.birthday"
        @close="isUpdateBirthdayShow = false"
      />
    </van-popup>
    <!-- 上传头像 -->
    <input type="file" hidden ref="file" @change="onFileChange" />
    <!-- 编辑头像弹层 -->
    <van-popup
      v-model="isUpdatePhotoShow"
      :style="{ height: '100%' }"
      position="bottom"
    >
      <UpdatePhoto
        v-if="isUpdatePhotoShow"
        :img="img"
        @update-photo="user.photo = $event"
        @close="isUpdatePhotoShow = false"
      />
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from "@/api/user";
import UpdateName from "./components/update-name.vue";
import UpdateGender from "./components/update-gender.vue";
import UpdateBirthday from "./components/update-birthday.vue";
import UpdatePhoto from "./components/update-photo.vue";
export default {
  name: "UserProfile",
  data() {
    return {
      user: {}, //个人信息
      isUpdateNameShow: false, //控制昵称弹层
      isUpdateGenderShow: false, //控制性别弹层
      isUpdateBirthdayShow: false, //控制生日弹层
      isUpdatePhotoShow: false, //控制头像弹层
      img: "",
    };
  },
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto,
  },
  created() {
    this.loadUserProfile();
  },
  methods: {
    async loadUserProfile() {
      try {
        const { data } = await getUserProfile();
        this.user = data.data;
      } catch (err) {
        this.$toast("获取用户个人资料失败");
      }
    },
    onFileChange() {
      const file = this.$refs.file.files[0];
      this.img = window.URL.createObjectURL(file);
      this.isUpdatePhotoShow = true;
      // file-input 如果选了同一个文件不会触发 change 事件
      // 解决办法就是每次使用完毕，把它的 value 清空
      this.$refs.file.value = "";
    },
  },
};
</script>


<style  scoped lang="less" >
.user-profile {
  .page-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__text {
      color: #fff;
      background-color: #3296fa;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background: #f5f7f9;
  }
}
</style>